package com.lry.jvm.instructions.base;

import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JThread;
import com.lry.jvm.rtda.heap.JClass;
import com.lry.jvm.rtda.heap.JMethod;

public class ClassInit {

    public static void initClass(JThread thread, JClass jClass) {
        jClass.setInitStarted(true);
        scheduleClinit(thread,jClass);
        initSuperClass(thread,jClass);
    }

    private static void scheduleClinit(JThread thread, JClass jClass) {
        JMethod clinit = jClass.getClinitMethod();
        if(clinit!=null){
            Frame newFrame = thread.newFrame(clinit);
            thread.pushFrame(newFrame);
        }
    }

    private static void initSuperClass(JThread thread, JClass jClass) {
        if(!jClass.isInterface()){
            JClass superClass = jClass.getSuperClass();
            if(null!=superClass && !superClass.isInitStarted()){
                initClass(thread,superClass);
            }
        }
    }
}
